//
//  CreateWalletVC.m
//  ShandsMod
//
//  Created by zh s on 2023/3/20.
//  Copyright © 2023 zh_s. All rights reserved.
//

#import "CreateWalletVC.h"
#import "GWPrivacyView.h"
#import "CustomWebviewVC.h"

@interface CreateWalletVC ()
/**! privacyView*/
@property (nonatomic, strong) GWPrivacyView *privacyView;

@end

@implementation CreateWalletVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"";
    self.view.backgroundColor = UIColor.whiteColor;
    
    UILabel *titleLab = [BaseViewFactory labelWithFrame:CGRectZero textColor:RGB(20, 28, 48) font:FONT_NAME(FONT_Medium, 21) textAligment:NSTextAlignmentCenter andtext:@"创建钱包"];
    [self.view addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(NAV_HEIGHT + 50);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.height.mas_equalTo(21);
    }];
    
    UILabel *explainLab = [BaseViewFactory labelWithFrame:CGRectZero textColor:MAIN_BACKCOLOR font:FONT_NAME(FONT_Regular, 15) textAligment:NSTextAlignmentCenter andtext:@"填写完成后，点击创建即可保存"];
    [self.view addSubview:explainLab];
    [explainLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLab.mas_bottom).mas_offset(21);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.height.mas_equalTo(15);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    [self.view addSubview:lineView];
    lineView.backgroundColor = LINEVIEWCELL_BACKCOLOR;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(31);
        make.top.mas_equalTo(titleLab.mas_bottom).mas_offset(84);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 54, 0.5));
    }];
    
    
    
    UIButton *createBtn = [BaseViewFactory buttonWithFrame:CGRectZero font:FONT_NAME(FONT_Medium, 16) title:@"创建钱包" titleColor:UIColor.whiteColor backColor:MAIN_ACTIVE_COLOR];
    [self.view addSubview:createBtn];
    [createBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(35);
        make.top.mas_equalTo(titleLab.mas_bottom).mas_offset(338);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 70, 40));
    }];
    GWRadius(createBtn, 20);
    [createBtn addTarget:self action:@selector(createWalletClick) forControlEvents:UIControlEventTouchUpInside];
    
    GWPrivacyView *privacyView = [[GWPrivacyView alloc] init];
    [self.view addSubview:privacyView];
    [privacyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX).mas_offset(15);
        make.top.mas_equalTo(createBtn.mas_bottom).mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 70, 40));
    }];
//    privacyView.backgroundColor = RandomColor;
    WS(weakSelf);
    privacyView.privacyViewBlock = ^{
        [weakSelf privacyBtnClick];
    };
    _privacyView = privacyView;

    
    NSArray *titleArr = @[@"钱包名称",@"设置密码",@"再次输入",@"密码提示"];
    NSArray *placeholderArr = @[@"设置钱包名称（以太坊）",@"请输入密码",@"请再次输入密码",@"非必填"];
    for (int i = 0; i <titleArr.count; i ++) {
        UIView *cellV = [[UIView alloc] init];
        [self.view addSubview:cellV];
//        cellV.backgroundColor = RandomColor;
        //cellV.frame = CGRectMake(31, CGRectGetMaxY(lineView.frame), SCREEN_WIDTH - 54, 60);
        [cellV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(lineView.mas_bottom).mas_offset(60 *i);
            make.left.mas_equalTo(31);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 54, 60));
        }];
        
        UIView *cellLine = [[UIView alloc] init];
        [cellV addSubview:cellLine];
        cellLine.backgroundColor = LINEVIEWCELL_BACKCOLOR;
        [cellLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.bottom.mas_equalTo(cellV.mas_bottom);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 54, 0.5));
        }];

        UILabel *titleLab = [BaseViewFactory labelWithFrame:CGRectZero textColor:RGB(20, 28, 48) font:FONT_NAME(FONT_Medium, 12) textAligment:NSTextAlignmentLeft andtext:titleArr[i]];
        [cellV addSubview:titleLab];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(7.5);
            make.bottom.mas_equalTo(cellV.mas_bottom).mas_offset(-18);
            make.height.mas_equalTo(15);
        }];

        UITextField *inputTF = [[UITextField alloc] init];
        [cellV addSubview:inputTF];
        inputTF.placeholder = placeholderArr[i];
        inputTF.textColor = MAIN_BACKCOLOR;
        inputTF.font = FONT_NAME(FONT_Medium, 12);

        inputTF.delegate = self;
        inputTF.tag = 100 +i;
        [inputTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(titleLab.mas_right).mas_offset(10);
            make.top.mas_equalTo(titleLab.mas_top).mas_offset(2);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 80, 13));
        }];
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:placeholderArr[i]];
        NSString *placeholderStr = placeholderArr[i];

            [placeholder addAttribute:NSForegroundColorAttributeName

                                value:RGB(175, 182, 203)

                                range:NSMakeRange(0, placeholderStr.length)];

        inputTF.attributedPlaceholder = placeholder;
       
       
//        inputTF.keyboardType = UIKeyboardTypeNumberPad;

        UIImageView *iconImg = [[UIImageView alloc] init];
        [cellV addSubview:iconImg];
        iconImg.image = IMAGE_NAME(@"icon_tableview_star");
        [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(titleLab.mas_left).mas_offset(-2);
            make.centerY.mas_equalTo(titleLab.mas_centerY).mas_offset(-2);
            make.height.width.mas_equalTo(6);
        }];
        
        if ( i == 3) {
            iconImg.hidden = YES;
        }else if (i == 1) {
            UIButton *hidePassword = [BaseViewFactory buttonWithHeight:16 imagePath:@"icon_password_hide"];
            [cellV addSubview:hidePassword];
            [hidePassword mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(cellV.mas_right).mas_offset(-10);
                make.centerY.mas_equalTo(titleLab.mas_centerY);
                make.size.mas_equalTo(CGSizeMake(16, 16));
            }];
            [hidePassword addTarget:self action:@selector(hidePasswordClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
}


#pragma mark ----- 点击事件
-(void)privacyBtnClick {
    
//http://sxe.shands.cn/ysbh.html
    CustomWebviewVC *vc = [[CustomWebviewVC alloc] init];
    vc.navTitleStr = @"使用者协议";
    vc.isShowNav = YES;
    vc.urlStr = @"http://sxe.shands.cn/ysbh.html";
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)createWalletClick {
    if (_privacyView.selectedBtn.selected == NO) {
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"请阅读并同意《隐私政策》" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *checkAction = [UIAlertAction actionWithTitle:@"查看条款" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            if ([self.delegate respondsToSelector:@selector(didTapPrivacyBtn)]) {
//                [self.delegate didTapPrivacyBtn];
//            }
        }];
        [alertVc addAction:checkAction];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"同意并登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            _privacyView.selectedBtn.selected = YES;
//            [self loginIn];
        }];
        [alertVc addAction:okAction];
//        [[ToolsClass getCurrentVC] presentViewController:alertVc animated:YES completion:nil];
        return;
    }

}

- (void)hidePasswordClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    UITextField *passwordTF = [self.view viewWithTag:101];
    passwordTF.secureTextEntry = sender.selected;
}

@end
